<?php

declare(strict_types=1);

namespace App\Admin;

use App\Entity\AdminUser;
use App\Entity\Menu;
use App\Entity\PageCategory;
use App\Utils\Helper\GetterHelper;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Templating\TemplateRegistry;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Security\Core\Security;

final class MenuAdmin extends AbstractAdmin {
    protected $baseRouteName = 'admin_menu';
    protected $baseRoutePattern = 'menu';
    protected $maxPageLinks = 10;
    protected $security;

    public function __construct ($code, $class, $baseControllerName, Security $security) {
        parent::__construct($code, $class, $baseControllerName);
        $this->security = $security;
    }

    protected function configureDefaultSortValues (array &$sortValues) {
        $sortValues['_page'] = 1;
        $sortValues['_per_page'] = 32;
        $sortValues['_sort_order'] = 'DESC';
        $sortValues['_sort_by'] = 'position';
    }

    public function toString ($object) {
        return $object instanceof Menu ? $object->getTitle() : '菜单';
    }

    protected function configureDatagridFilters (DatagridMapper $datagridMapper): void {
        $datagridMapper
            ->add('title', null, ['label' => '名称'])
            ->add('url', null, ['label' => '外链'])
            ->add('alias', null, ['label' => '别名']);
    }

    protected function configureListFields (ListMapper $listMapper): void {
        $listMapper
            ->addIdentifier('title', null, ['label' => '名称'])
            ->add('alias', null, ['label' => '别名'])
            ->add('url', null, [
                'label'    => '外链',
                'template' => 'backend/menu/list_url.html.twig',
            ])
            ->add('page_category_id', null, [
                'label'         => '文章类别',
                'page_category' => GetterHelper::getEntityManager()->getRepository(PageCategory::class)->findAllToArray(),
                'template'      => 'backend/public/list_page_category_id.html.twig',
            ])
            ->add('style', TemplateRegistry::TYPE_CHOICE, [
                'label'        => '方式',
                'choices'      => [
                    'list'  => '列表',
                    'image' => '图片',
                ],
                'template'     => 'backend/menu/list_style.html.twig',
                'header_style' => 'width: 7%; text-align: left',
                'row_align'    => 'left',
            ])
            ->add('position', null, [
                'label' => '排序',
                'header_style' => 'width: 5%; text-align: left',
                'row_align'    => 'left',
            ])
            ->add('admin_user_id', null, [
                'label'       => '创建人',
                'admin_users' => GetterHelper::getEntityManager()->getRepository(AdminUser::class)->findAllToArray(),
                'template'    => 'backend/public/list_admin_user_id.html.twig',
            ])
            ->add('created_at', null, [
                'label'        => '日期',
                'format'       => 'Y-m-d H:i:s',
                'timezone'     => 'PRC',
                'header_style' => 'width: 13%; text-align: left',
                'row_align'    => 'left',
            ]);
    }

    protected function configureFormFields (FormMapper $formMapper): void {
        $formMapper
            ->add('title', null, ['label' => '名称'])
            ->add('alias', null, ['label' => '别名'])
            ->add('description', null, ['label' => '描述'])
            ->add('url', null, [
                'label' => '外链',
                'help'  => '添加外链后，则直接跳转到对应链接，请确保链接正确，如：http://www.xxx.com',
            ])
            ->add('page_category_id', ChoiceType::class, [
                'label'    => '文章类别',
                'choices'  => array_flip(GetterHelper::getEntityManager()->getRepository(PageCategory::class)->findAllToArray()),
                'required' => false,
            ])
            ->add('style', ChoiceType::class, [
                'label'    => '方式',
                'choices'  => [
                    '列表' => 'list',
                    '图片' => 'image',
                ],
                'help'     => '菜单内容展现的方式',
                'required' => false,
            ])
            ->add('position', null, [
                'label' => '排序',
                'help'  => '数字越大越靠前显示',
            ]);
    }

    protected function configureShowFields (ShowMapper $showMapper): void {
        $showMapper
            ->add('id')
            ->add('title')
            ->add('url')
            ->add('page_category_id')
            ->add('style')
            ->add('position')
            ->add('admin_user_id')
            ->add('created_at')
            ->add('updated_at');
    }

    protected function configureRoutes (RouteCollection $collection) {
        parent::configureRoutes($collection); // TODO: Change the autogenerated stub
        $collection->remove('show');
        $collection->remove('export');
    }

    public function prePersist ($object) {
        parent::prePersist($object); // TODO: Change the autogenerated stub

        $object->setAdminUserId($this->security->getUser()->getId());
        $object->setCreatedAt(new \DateTime());
    }
}
